package com.etime.shycourse.controller.wx.kf;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shy.common.BaseMessage;
import com.etime.shy.common.ListMessage;
import com.etime.shycourse.config.PageMessage;
import com.etime.shycourse.config.Tools;
import com.etime.shycourse.dto.CourseAdviceLogDto;
import com.etime.shycourse.dto.CurrentUser;
import com.etime.shycourse.pojo.CourseAdviceLog;
import com.etime.shycourse.pojo.Organization;
import com.etime.shycourse.services.interfaces.CourseAdviceLogService;
import com.etime.shycourse.services.interfaces.OrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 微信-（后台）-微信客服相关数据接口
 * 小程序端调用添加客服记录
 * 后台进行客服咨询统计
 */
@Api(tags = "微信-（后台）-微信客服相关数据接口")
@RestController
@RequestMapping("/wx/advice")
public class CourseAdviceLogController {

    @Autowired
    private CourseAdviceLogService courseAdviceLogService;

    @Autowired
    private OrganizationService organizationService;

    @Autowired
    private Tools tools;

    /**
     * 添加课程咨询记录
     *
     * @param log
     * @return
     */
    @ApiOperation("添加课程咨询记录--微信小程序端调用（需要授权登录调用）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adviceid", value = "咨询记录id-不用传递"),
            @ApiImplicitParam(name = "advicecourseid", value = "咨询课程id"),
            @ApiImplicitParam(name = "advicecoursename", value = "咨询课程名称"),
            @ApiImplicitParam(name = "adviceuserid", value = "咨询人id-不用传递"),
            @ApiImplicitParam(name = "adviceusername", value = "咨询人姓名(真实姓名+昵称)-不用传递"),
            @ApiImplicitParam(name = "adviceuseropenid", value = "咨询人openid-不用传递"),
            @ApiImplicitParam(name = "adviceorgid", value = "被咨询机构id"),
            @ApiImplicitParam(name = "adviceorgname", value = "被咨询机构名称-不用传递"),
            @ApiImplicitParam(name = "adviceaccount", value = "被咨询客服(客服url）"),
            @ApiImplicitParam(name = "advicetime", value = "开始咨询时间-不用传递")
    })
    @PostMapping("/insert")
    public BaseMessage insertCourseAdviceLog(@RequestBody CourseAdviceLog log, HttpServletRequest request) {
        BaseMessage msg = new BaseMessage();
        //验证token
        String token = request.getHeader("token");
        CurrentUser currentUser = tools.getCurrentUserFromRedis(token);
        if (currentUser == null) {
            msg.setCode(-2);
            msg.setMsg("未获取到用户信息；可能登录超时或者非法访问。");
        } else {
            //获取机构名称
            Organization org = organizationService.selectOrganizationById(log.getAdviceorgid());
            //插入课程咨询记录
            log.setAdviceuserid(currentUser.getUser().getUserid());
            log.setAdviceusername(currentUser.getUser().getUsertruename() + "(" + currentUser.getUser().getNickname() + ")");
            log.setAdviceuseropenid(currentUser.getUser().getOpenid());
            log.setAdviceorgname(org.getOrgname());
            log.setAdvicetime(new Date());
            int rtn = courseAdviceLogService.insertCourseAdviceLog(log);
            if (rtn > 0) {
                msg.setCode(rtn);
                msg.setMsg("保存课程咨询记录成功");
            } else {
                msg.setCode(0);
                msg.setMsg("保存课程咨询记录失败");
            }
        }
        return msg;
    }

    @ApiOperation("查询课程咨询记录 分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少行"),
            // 复用
            @ApiImplicitParam(name = "advicecoursename", value = "课程名称"),
            @ApiImplicitParam(name = "adviceorgid", value = "机构id"),
            @ApiImplicitParam(name = "one", value = "开始时间"),
            @ApiImplicitParam(name = "two", value = "结束时间"),
    })
    @GetMapping("/page")
    public PageMessage<CourseAdviceLog> selectCourseAdviceLogByPage(CourseAdviceLogDto log, Long pageNum, Long pageSize) {
        PageMessage<CourseAdviceLog> msg = new PageMessage<>();
        Page<CourseAdviceLog> data = courseAdviceLogService.selectCourseAdviceLogByPage(log, pageNum, pageSize);
        if (data != null) {
            msg.setCode(1);
            msg.setPagedata(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("机构查询课程咨询记录 分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "第几页"),
            @ApiImplicitParam(name = "pageSize", value = "每页多少行"),
            // 复用
            @ApiImplicitParam(name = "advicecoursename", value = "课程名称"),
            @ApiImplicitParam(name = "adviceorgid", value = "机构id"),
            @ApiImplicitParam(name = "one", value = "开始时间"),
            @ApiImplicitParam(name = "two", value = "结束时间"),
    })
    @GetMapping("/organization/page")
    public PageMessage<CourseAdviceLog> selectCourseAdviceLogByPages(HttpServletRequest request, CourseAdviceLogDto log, Long pageNum, Long pageSize) {
        PageMessage<CourseAdviceLog> msg = new PageMessage<>();
        //验证toke
        String token = request.getHeader("token");
        CurrentUser currentUser = tools.getCurrentUserFromRedis(token);
        if (currentUser == null) {
            msg.setCode(-2);
            msg.setMsg("未获取到用户信息；可能登录超时或者非法访问。");
        } else {
            log.setAdviceorgid(currentUser.getOrgid());
            Page<CourseAdviceLog> data = courseAdviceLogService.selectCourseAdviceLogByPage(log, pageNum, pageSize);
            if (data != null) {
                msg.setCode(1);
                msg.setPagedata(data);
            } else {
                msg.setCode(0);
                msg.setMsg("没有查询到数据");
            }
        }
        return msg;
    }

    @GetMapping("/organization/count/page")
    @ApiOperation("机构分页查询课程咨询记录的统计")
    public PageMessage<CourseAdviceLogDto> selectCourseAdviceLogAdviceUserCountPages(HttpServletRequest request, CourseAdviceLogDto log, Long pageNum, Long pageSize) {
        PageMessage<CourseAdviceLogDto> msg = new PageMessage<>();
        //验证toke
        String token = request.getHeader("token");
        CurrentUser currentUser = tools.getCurrentUserFromRedis(token);
        if (currentUser == null) {
            msg.setCode(-2);
            msg.setMsg("未获取到用户信息；可能登录超时或者非法访问。");
        } else {
            log.setAdviceorgid(currentUser.getOrgid());
            Page<CourseAdviceLogDto> data = courseAdviceLogService.selectCourseAdviceLogAdviceUserCountPage(log, pageNum, pageSize);
            if (data != null) {
                msg.setCode(1);
                msg.setPagedata(data);
            } else {
                msg.setCode(0);
                msg.setMsg("没有查询到数据");
            }
        }
        return msg;
    }

    @GetMapping("/count/page")
    @ApiOperation("分页查询课程咨询记录的统计")
    public PageMessage<CourseAdviceLogDto> selectCourseAdviceLogAdviceUserCountPage(CourseAdviceLogDto log, Long pageNum, Long pageSize) {
        PageMessage<CourseAdviceLogDto> msg = new PageMessage<>();
        Page<CourseAdviceLogDto> data = courseAdviceLogService.selectCourseAdviceLogAdviceUserCountPage(log, pageNum, pageSize);
        if (data != null) {
            msg.setCode(1);
            msg.setPagedata(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有查询到数据");
        }
        return msg;
    }

    @ApiOperation("查询机构 课程咨询的总记录")
    @GetMapping("/organization/count/list")
    public ListMessage<CourseAdviceLogDto> selectCourseAdviceLogOrgCounts(HttpServletRequest request, CourseAdviceLogDto log) {
        ListMessage<CourseAdviceLogDto> msg = new ListMessage<>();
        //验证toke
        String token = request.getHeader("token");
        CurrentUser currentUser = tools.getCurrentUserFromRedis(token);
        if (currentUser == null) {
            msg.setCode(-2);
            msg.setMsg("未获取到用户信息；可能登录超时或者非法访问。");
        } else {
            log.setAdviceorgid(currentUser.getOrgid());
            List<CourseAdviceLogDto> data = courseAdviceLogService.selectCourseAdviceLogOrgCount(log);
            if (data != null) {
                msg.setCode(1);
                msg.setDatalist(data);
            } else {
                msg.setCode(0);
                msg.setMsg("没有机构咨询统计数据");
            }
        }
        return msg;
    }

    @ApiOperation("查询所有机构 课程咨询的总记录")
    @GetMapping("/count/list")
    public ListMessage<CourseAdviceLogDto> selectCourseAdviceLogOrgCount(CourseAdviceLogDto log) {
        ListMessage<CourseAdviceLogDto> msg = new ListMessage<>();
        List<CourseAdviceLogDto> data = courseAdviceLogService.selectCourseAdviceLogOrgCount(log);
        if (data != null) {
            msg.setCode(1);
            msg.setDatalist(data);
        } else {
            msg.setCode(0);
            msg.setMsg("没有机构咨询统计数据");
        }
        return msg;
    }
}
